//
//  AWHomeRetweetView.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/8.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit
import SnapKit

class AWHomeRetweetView: UIView {

    var statusesViewModel: AWStatusesViewModel? {
        
        didSet {
            
            contentLabel.text = statusesViewModel?.retweetContent
            
            retweeteBottomConstraints?.deactivate()
            
            if let picUrls = statusesViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                
                pictureView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                    
                    retweeteBottomConstraints = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                    
                })
                
                pictureView.picUrls = picUrls
                
            } else {
                
                pictureView.isHidden = true
                
                self.snp.makeConstraints({ (make) in
                    
                    retweeteBottomConstraints = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                    
                })
                
            }
                        
        }
        
    }
    
    var retweeteBottomConstraints: Constraint?
    
    fileprivate lazy var contentLabel: UILabel = UILabel(title: "test", fontSize: 13, textColor: UIColor.lightGray, maxWidth: nil)
    
    fileprivate lazy var pictureView: AWHomePictureView = {
    
        let collctionView = AWHomePictureView()
        
        collctionView.backgroundColor = self.backgroundColor
        
        return collctionView
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        contentLabel.textAlignment = .natural
        
        contentLabel.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
            
        }
        
        pictureView.snp.makeConstraints { (make) in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
        }
        
        self.snp.makeConstraints { (make) in
            
            retweeteBottomConstraints = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
            
        }
        
    }

}
